package com.example.result.preference

import android.content.Intent
import android.net.Uri
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.Send
import androidx.compose.material3.Button
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import com.example.result.ui.theme.AppTheme

@Composable
fun SettingScreen(modifier: Modifier) {
    AppTheme {
        Surface(modifier = modifier) {
            val context = LocalContext.current
            var ip by remember {
                mutableStateOf(Config.getIp(context) ?: "")
            }
            var port by remember {
                mutableStateOf(Config.getPort(context) ?: "")
            }
            var upLoadPort by remember {
                mutableStateOf(Config.getUploadPort(context) ?: "")
            }
            LazyColumn {
                item {
                    Button(onClick = {
                        context.startActivity(Intent(Intent.ACTION_VIEW).apply {
                            data = Uri.parse("http://${ip}:${port}")
                        })
                    }) {
                        Text(text = "访问下载网址")
                    }
                }
                item {
                    Button(onClick = {
                        context.startActivity(Intent(Intent.ACTION_VIEW).apply {
                            data = Uri.parse("http://${ip}:${upLoadPort}")
                        })
                    }) {
                        Text(text = "访问上传网址")
                    }
                }
                item {
                    TextField(value = ip, onValueChange = {
                        ip = it
                    }, trailingIcon = {
                        IconButton(onClick = { Config.setIp(context, ip) }) {
                            Icon(imageVector = Icons.AutoMirrored.Filled.Send, "")
                        }
                    }, label = {
                        Text(text = "ip")
                    })
                }
                item {
                    TextField(value = port, onValueChange = {
                        port = it
                    }, trailingIcon = {
                        IconButton(onClick = { Config.setPort(context, port) }) {
                            Icon(imageVector = Icons.AutoMirrored.Filled.Send, "")
                        }
                    }, label = {
                        Text(text = "port")
                    })
                }
                item {
                    TextField(value = upLoadPort, onValueChange = {
                        upLoadPort = it
                    }, trailingIcon = {
                        IconButton(onClick = { Config.setUploadPort(context, upLoadPort) }) {
                            Icon(imageVector = Icons.AutoMirrored.Filled.Send, "")
                        }
                    }, label = {
                        Text(text = "uploadPort")
                    })
                }
            }
        }
    }
}